/*
*mvcAs3Player
*chenjie
*2012-5-8
*/
package
{
	import com.pl.model.Data;
	import com.pl.model.PlayModel;
	
	import flash.display.Sprite;
	
	public class MVCAs3Player extends Sprite
	{
		public function MVCAs3Player()
		{
			
		}
		
		public function init():void{
			initJs()
			initData()
			
			controlBar=new ControlBar()
			
			player=new MainFlvPlayer(controlBar)
			player.addEventListener(PlayerEvent.RESET,playerReset)
			player.addEventListener(PlayerEvent.PLAYER_STATE,doChangeState)
			player.addEventListener(PlayerEvent.TIME,adDotime)
			addChild(player)
			
			adSprite=new ADSprite()
			adSprite.addEventListener(Event.COMPLETE,adComplete)
			adSprite.addEventListener(PlayerEvent.TIME,adDotime)
			addChild(adSprite)	
			
			controlBar.x=this.x
			controlBar.y=player.y+player.height+5
			
			controlBar.addEventListener(PlayerEvent.PLAY,doPlaying)
			controlBar.addEventListener(PlayerEvent.SHOW_SHARE,doShare)
			controlBar.visible=false
			addChild(controlBar)
			
			screenBar=new ScreenBar()
			screenBar.addEventListener(PlayerEvent.SET_SCALE,playerSetScale)
			addChild(screenBar)
			
			sharePanel=new SharePanel(stage)
			addChild(sharePanel)
			
			adTextSp=new AdTextSprite()
			adTextSp.addEventListener(PlayerEvent.PLAY_CONTENT,doPlayContent)
			addChild(adTextSp)
			adTextSp.x=Data.playerWidth-adTextSp.width
			
			dataLoader=new DataLoader()
			dataLoader.addEventListener(Event.COMPLETE,loadComplete)
			
			text.defaultTextFormat=new TextFormat("",18,0xffffff)
			text.text="ssdasdadasd"
			
		}
		
		//初始化 数据类
		public function initData():void{
			Data.initBitmapDataArr(new BtnBitmap as DisplayObject)
		}
		
		public function receivedFromJavaScript(param1:String):void{
			var playModel:PlayModel=new PlayModel(param1,"flv","","content")
			Data.playArr.push(playModel)	
			startPlay()
		}
		
		//开始播放
		public function startPlay():void{
			if(Data.playArr.length >0 && Data.playArr[Data.playArr.length-1].name=="content"){
				var nextPlayModel:PlayModel=Data.playArr.shift()
				play(nextPlayModel)
			}
		}
		
		//播放前的检查
		public function play(playModel:PlayModel):void{
			if(playModel){
				switch(playModel.type)
				{
					case "flv":
					{
						player.playPlayModel(playModel)
						controlBar.visible=true
						adTextSp.visible=false	
						break;
					}
					case "f4v":
					{
						player.playPlayModel(playModel)
						controlBar.visible=true
						adTextSp.visible=false	
						break;
					}
					case "swf":
					{
						adSprite.setPlayModel(playModel)
						break;
					}
					case "jpg":
					{
						adSprite.setPlayModel(playModel)
						break;
					}
						
					default:
					{
						break;
					}
				}
			}
		}
		
	}
}